package com.banmao.mall.controller.client;

import com.banmao.mall.model.bo.client.cart.CartAddBO;
import com.banmao.mall.model.bo.client.cart.CartCheckedBO;
import com.banmao.mall.model.bo.client.cart.CartUpdateBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.client.cart.CartCheckoutVO;
import com.banmao.mall.model.vo.client.cart.CartIndexVO;
import com.banmao.mall.service.client.CartService;
import com.banmao.mall.util.CustomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/cart")
public class CartController {

    @Autowired
    CartService cartService;

    //获取购物车的数据 wx/cart/index
    @GetMapping("/index")
    public BaseRespVO cartIndex() throws IllegalAccessException {
       Integer userId = CustomUtils.judgeUserLogin();
//        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute("userId");
//        if(userId==null){
//            BaseRespVO.error("",401);
//        }
        CartIndexVO cartIndexVO = cartService.getCartList(userId);
        return BaseRespVO.ok(cartIndexVO);
    }

    //添加商品到购物车
    @PostMapping("/add")
    public BaseRespVO cartAdd(@RequestBody CartAddBO cartAddBO) throws IllegalAccessException {
        Integer userId = CustomUtils.judgeUserLogin();
        Boolean result = cartService.addCart(cartAddBO,userId);
        //返回购物车中存在商品数
        Integer cartCount = cartService.getCartCount(userId);
        if (result == false){
            return BaseRespVO.error("添加出错",404);
        }else {
            return BaseRespVO.ok(cartCount);
        }
    }

    //获取购物车商品件数 wx/cart/goodscount
    @GetMapping("goodscount")
    public BaseRespVO cartGoodsCount()  {
        Integer userId = null;
        try {
            userId = CustomUtils.judgeUserLogin();
        } catch (IllegalAccessException e) {
            //e.printStackTrace();
        }
        Integer total = cartService.cartGoodsCount(userId);
        if (total == null){
            return BaseRespVO.ok(0);
        }
        return BaseRespVO.ok(total);
    }

    //更新购物车的商品 wx/cart/update
    @PostMapping("update")
    public BaseRespVO cartUpdate(@RequestBody CartUpdateBO cartUpdateBO) throws IllegalAccessException {
        Integer userId = CustomUtils.judgeUserLogin();
        Integer result = cartService.cartUpdate(userId,cartUpdateBO);
        if (result == 0){
            return BaseRespVO.error("更新失败,库存不足",404);
        }
        return BaseRespVO.ok(null);
    }

    //删除购物车的商品 wx/cart/delete
    @PostMapping("delete")
    public BaseRespVO cartDelete(@RequestBody Map<String,Object> map) throws IllegalAccessException {
        Integer userId = CustomUtils.judgeUserLogin();
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        Integer result = cartService.cartDelete(userId,productIds);
        if (result == 0){
            BaseRespVO.error("删除失败",404);
        }
        CartIndexVO cartIndexVO = cartService.getCartList(userId);
        return BaseRespVO.ok(cartIndexVO);
    }
    //--------------------------------------------------------------------------------------------------------
    //选择或取消选择商品 wx/cart/checked
    @PostMapping("/checked")
    public BaseRespVO cartChecked(@RequestBody CartCheckedBO cartCheckedBO) throws IllegalAccessException {
        Integer userId = CustomUtils.judgeUserLogin();
        Boolean result = cartService.checkedCart(cartCheckedBO,userId);
        if (result == false){
            return BaseRespVO.error("修改失败",404);
        }
        CartIndexVO cartIndexVO = cartService.getCartList(userId);
        return BaseRespVO.ok(cartIndexVO);
    }
    //-------------------------------------------------------------------------------------------------------------------
    //下单前信息确认 wx/cart/checkout
    //cartId=0&addressId=7&couponId=0&grouponRulesId=4
    @GetMapping("checkout")
    public BaseRespVO cartCheckout(Integer cartId,Integer addressId,Integer couponId,Integer grouponRulesId) throws IllegalAccessException {
        Integer userId = CustomUtils.judgeUserLogin();
        CartCheckoutVO cartCheckoutVO = cartService.checkout(userId,cartId,addressId,couponId,grouponRulesId);
        return BaseRespVO.ok(cartCheckoutVO);
    }

    //立即购买商品 wx/cart/fastadd
    @PostMapping("fastadd")
    public BaseRespVO cartFastAdd(@RequestBody CartAddBO cartAddBO) throws IllegalAccessException {
        Integer userId = CustomUtils.judgeUserLogin();
        Integer cartId = cartService.fastAdd(cartAddBO,userId);
        if (cartId != null) {
            return BaseRespVO.ok(cartId);
        } else {
            return BaseRespVO.error("购买失败",404);
        }
    }
}
